package com.example.pro1;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import org.jsoup.nodes.Element;

import java.text.Format;
import java.util.ArrayList;
import java.util.HashMap;

public class AdapterTryActivity extends ListActivity implements Runnable, AdapterView.OnItemClickListener{
    Handler handler;
    public static final String TAG = "AdapterTryActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*
        ArrayList<HashMap<String, String>> info = new ArrayList<HashMap<String, String>>();
        for(int i=1; i<=10; i++) {
            HashMap<String, String> tmp = new HashMap<String, String>();
            tmp.put("LeftTop", "LeftTop"+i);
            tmp.put("RightBottom", "RightBottom"+i);
            info.add(tmp);
        }

        SimpleAdapter infoadapter = new SimpleAdapter(this, info, R.layout.adpter_try,
                new String[] {"LeftTop", "RightBottom"}, new int[] {R.id.adapterTVLeftTop, R.id.adapterTVRightBottom});
        setListAdapter(infoadapter);
        */
        handler = new Handler(Looper.myLooper()) {

            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                if(msg.what == 1) {

                    ArrayList<HashMap<String, String>> info = (ArrayList<HashMap<String, String>>) msg.obj;
                    MyRateAdapter adapter = new MyRateAdapter(AdapterTryActivity.this, R.layout.adpter_try, info);
                    setListAdapter(adapter);
                }
            }
        };

        Thread t = new Thread(this);
        t.start();

        getListView().setOnItemClickListener(this);
    }

    @Override
    public void run() {
        String url = "https://www.huilvzaixian.com/";
        ArrayList<HashMap<String, String>> info = new ArrayList<HashMap<String, String>>();
        try {
            org.jsoup.nodes.Element RateListViewEle = MyTools.getEleOfUrl(url, "class", "history-urls", 0);
            for(Element RateListViewRate:RateListViewEle.getElementsByTag("a")) {
                HashMap<String, String> tmp = new HashMap<String, String>();
                String[] RateListViewTmp = RateListViewRate.text().split(" ");
                tmp.put("country", RateListViewTmp[1]);
                tmp.put("rate", RateListViewTmp[2]);

                info.add(tmp);
            }
        }catch (Exception e) {
            Log.i("AdapterTryActivity", "run: " + e);
        }

        Message msg = new Message();
        msg.what = 1;
        msg.obj = info;
        handler.sendMessage(msg);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        HashMap<String, String> info = (HashMap<String, String>) getListView().getItemAtPosition(position);

        Intent intent = new Intent(this, AllRateActivity.class);
        intent.putExtra("country", info.get("country"));
        intent.putExtra("rate", info.get("rate"));
        startActivity(intent);
    }
}